<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Session;
class Share extends Controller{
    /****
     * Class register          	分享
     * @$oneself_id[int]   		自身ID
     * @$superior_id[int]   	上级ID
     * @date                	2020/05/19 
     * @package application\index\controller\Share
    */
    public function index(){
        $oneself_id = 36;
        $superior_id = 1;
       
        if($superior_id == null){
        	$superior_id=0;
        }
        $user = Db::name('relation')->where(['oneself_id'=>$oneself_id])->find();
        if($user){
            $arr = [
               'state'     => 0,
               'message'   => "已是代理！"
            ];
            return json($arr);
        }
        $data = [
        	"oneself_id"	=>$oneself_id,
        	"superior_id"	=>$superior_id,
        	"relation_time"	=>time()
        ];
        $relation = Db::name('relation')->insert($data);
        $statistics = Db::name('statistics')->where(['user_id'=>$superior_id])->find();
        if($statistics){
        	Db::name('statistics')
        	->where(['user_id' => $statistics['user_id']])
    		->update(['nnt'=>$statistics['nnt'] + 1]);
        }else{
        	Db::name('statistics')
        	->insert(['user_id'=>$superior_id,'nnt'=>1]);
        }
        $arr = [
           'state'     => 1,
           'message'   => "请求成功"
        ];
        return json($arr);
    }
    /****
     * Class register          	分销规则
     * @$oneself_id[int]   		自身ID
     * @date                	2020/05/19 
     * @package application\index\controller\Share
    */
    public function distributor(){
    	$oneself_id = 1;
        $distributor_id = Db::name('distributor')->where(['distributor_id'=>$oneself_id])->find();
        if($distributor_id){
            $arr = [
               'state'     => 0,
               'message'  => "已是分销商请勿重复注册！"
            ];
            return json($arr);
        }
    	Db::name('distributor')->insert(['distributor_id'=>$oneself_id]);
        if($distributor_id){
            $arr = [
               'state'     => 0,
               'message'  => "注册成功"
            ];
        }else{
            $arr = [
               'state'     => 0,
               'message'  => "注册失败"
            ];
        }
        return json($arr);
    }
    /****
     * Class register           分销规则
     * @$oneself_id[int]        自身ID
     * @date                    2020/05/19 
     * @package application\index\controller\Share
    */
    public function generalize_url(){
        $user_id = input('user_id'); 
        $url = 'public/index/share/index/superior_id/'.$user_id;
        // $url ="http://www.baidu.com";
        // $this->redirect($url);
         $arr = [
           'state'     => 1,
           'url'       =>$url,
           'message'   => "请求成功"
        ];
        return json($arr);
    }

    public function group(){
        $user_id = input('user_id');
        $statistics = Db::name('statistics')->where(['user_id'=>$user_id])->find();
        $relation = Db::name('relation a')
                  ->where(['a.superior_id'=>$user_id])
                  ->join('user b','a.oneself_id = b.id','LEFT')
                  ->field('b.nickname,b.avatar,a.relation_time')
                  ->select();
         $arr = [
           'state'            => 1,
           'relation'         =>$relation,
           'statistics'       =>$statistics,
           'message'          => "请求成功"
        ];
        return json($arr);
    }
    /****
     * Class register           收益记录
     * @$oneself_id[int]        自身ID
     * @date                    2020/05/19 
     * @package application\index\controller\Share
    */
    public function statistics_log(){
        $post = input('post.');
        if($post['type'] == 0){
            $statistics_log = Db::name('statistics_log')
                            ->where(['user_id'=>$post['user_id']])
                            ->order('addtime desc')
                            ->paginate(8);
        }else{
            $statistics_log = Db::name('statistics_log')
                            ->where(['user_id'=>$post['user_id'],'type'=>$post['type']])
                            ->order('addtime desc')
                            ->paginate(8);
         
        }
         $arr = [
           'state'                  => 1,
           'statistics_log'         => $statistics_log,
           'message'                => "请求成功11"
        ];
        return json($arr);
    }
    

}
